package com.yun.algorithmproblem.leetcode;

import java.util.*;

import javafx.util.Pair;

public class Leetcode2353 {

    static class FoodRatings {

        private Map<String, Pair<Integer, String>> foodMap = new HashMap<>();
        private Map<String, TreeSet<Pair<Integer, String>>> cuisineMap = new HashMap<>();
        private int n;

        public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
            n = foods.length;
            for (int i = 0; i < n; i++) {
                String food = foods[i];
                String cuisine = cuisines[i];
                int rating = ratings[i];
                foodMap.put(food, new Pair<>(rating, cuisine));
                cuisineMap.computeIfAbsent(cuisine, k -> new TreeSet<>((a, b) ->
                        Objects.equals(a.getKey(), b.getKey()) ? a.getValue().compareTo(b.getValue()) : b.getKey() - a.getKey()))
                        .add(new Pair<>(rating, food));
            }
        }

        public void changeRating(String food, int newRating) {
            Pair<Integer, String> foodPair = foodMap.get(food);
            TreeSet<Pair<Integer, String>> cuisineTreeSet = cuisineMap.get(foodPair.getValue());
            cuisineTreeSet.remove(new Pair<>(foodPair.getKey(),food));
            cuisineTreeSet.add(new Pair<>(newRating,food));
            foodMap.put(food,new Pair<>(newRating,foodPair.getValue()));
        }

        public String highestRated(String cuisine) {
            return cuisineMap.get(cuisine).first().getValue();
        }
    }
}
